package com.example.springboot.druid.conf;

import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Arrays;

/**
 * @desc 创建druid数据源
 * @Author wangsh
 * @date 2018/5/6 12:12
 * @return
 */
@Configuration
public class DruidConfiguration {

	/**
	 * @return
	 * @desc 通过配置文件获取durid数据库配置，初始化数据源
	 * @Author wangsh
	 * @date 2018/5/6 12:02
	 */
	@ConfigurationProperties(prefix = "spring.datasource.druid")
	@Bean(initMethod = "init", destroyMethod = "close")
	public DruidDataSource dataSource() {
		DruidDataSource ds = new DruidDataSource();
		ds.setProxyFilters(Arrays.asList(statFilter()));
		return ds;
	}

	@Bean
	public Filter statFilter() {
		StatFilter filter = new StatFilter();
		filter.setSlowSqlMillis(5000);
		filter.setLogSlowSql(true);
		filter.setMergeSql(true);
		return filter;
	}
}
